/***************************************************
 @Name :        1196kk0
 @Time :        2025-05-26
 @Email :       2232506563@qq.com
 @Comment :     定时器初始化（PWM、中断）
 @FileName :    drv_TIM.c
 @Version :     4.0
****************************************************/

#include "drv_TIM.h"

/***************************************
函数名：drv_TIM7_Config
函数功能：定时器7初始化 配置中断控制温度数据刷新
输入参数：无
输出参数：无
其它： 
		分频=8400：计数1次需要的时间是100us
		装载值=10000；需要计的次数；
	  	定时时间:1s = 100*10000=1000000us
****************************************/
void drv_TIM7_Config(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseInitStruct;
    NVIC_InitTypeDef NVIC_InitStruct;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7 , ENABLE);
    
    TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInitStruct.TIM_Period = 10000-1;
    TIM_TimeBaseInitStruct.TIM_Prescaler = 8400-1;
    TIM_TimeBaseInit(TIM7 , &TIM_TimeBaseInitStruct);

    TIM_ITConfig(TIM7 , TIM_IT_Update , ENABLE);

    NVIC_InitStruct.NVIC_IRQChannel = TIM7_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStruct);

    TIM_Cmd(TIM7 , ENABLE);
}

/***************************************
函数名：drv_TIM6_Config
函数功能：定时器6初始化 配置中断控制Nixie刷新
输入参数：无
输出参数：无
其它： 
		分频=8400：计数1次需要的时间是100us
		装载值=20；需要计的次数；
	  	定时时间:2ms = 100*20=2000us
****************************************/
void drv_TIM6_Config(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseInitStruct;
    NVIC_InitTypeDef NVIC_InitStruct;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6 , ENABLE);
    
    TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInitStruct.TIM_Period = 20-1;            
    TIM_TimeBaseInitStruct.TIM_Prescaler = 8400-1;
    TIM_TimeBaseInit(TIM6 , &TIM_TimeBaseInitStruct);

    TIM_ITConfig(TIM6 , TIM_IT_Update , ENABLE);

    NVIC_InitStruct.NVIC_IRQChannel = TIM6_DAC_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStruct);

    TIM_Cmd(TIM6 , ENABLE);
}

/***************************************
函数名：drv_TIM13_Config
函数功能：定时器13初始化 配置PWM控制LED呼吸
输入参数：无
输出参数：无
其它： 
		分频=84：计数1次需要的时间是1us
		装载值=5000；需要计的次数；
	  	定时时间:5ms = 1*5000=5000us
****************************************/
void drv_TIM13_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
    TIM_OCInitTypeDef TIM_OCInitStruct;

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF , ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM13 , ENABLE);

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOF , &GPIO_InitStruct);

    GPIO_PinAFConfig(GPIOF , GPIO_PinSource8 , GPIO_AF_TIM13);

    TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInitStruct.TIM_Period = 5000-1;
    TIM_TimeBaseInitStruct.TIM_Prescaler = 84-1;
    TIM_TimeBaseInit(TIM13 , &TIM_TimeBaseInitStruct);
    TIM_ITConfig(TIM13 , TIM_IT_Update , ENABLE);

    TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;              //输出模式为PWM1
    TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_Low;       //有效电平为低
    TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;  //使能输出
    TIM_OCInitStruct.TIM_Pulse = 0;                             //比较初值
    TIM_OC1Init(TIM13 , &TIM_OCInitStruct);
    
    TIM_ARRPreloadConfig(TIM13 , ENABLE);               //开启装载寄存器的影子寄存器
    TIM_OC1PreloadConfig(TIM13 , TIM_OCPreload_Enable); //开启比较的影子寄存器
    TIM_CtrlPWMOutputs(TIM13 , ENABLE);                 //使能PWM输出

    TIM_Cmd(TIM13 , ENABLE);   //使能定时器13
}

/***************************************
函数名：drv_TIM12_Config
函数功能：定时器12初始化 配置PWM控制Fan转速
输入参数：无
输出参数：无
其它： 
		分频=84：计数1次需要的时间是1us
		装载值=5000；需要计的次数；
	  	定时时间:5ms = 1*5000=5000us
****************************************/
void drv_TIM12_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;


    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC , ENABLE);

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOC , &GPIO_InitStruct);

    GPIO_PinAFConfig(GPIOC , GPIO_PinSource6 , GPIO_AF_TIM12);

    TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInitStruct.TIM_Period = 5000-1;
    TIM_TimeBaseInitStruct.TIM_Prescaler = 84-1;
    TIM_TimeBaseInit(TIM12 , &TIM_TimeBaseInitStruct);
    TIM_ITConfig(TIM13 , TIM_IT_Update , ENABLE);                                   
}






